<?php
    session_start();
    include "../../function/functionPHP.php";
    include "../../class/crop.class.php";
    include "../../class/farm.class.php";
    include "../../class/rice.class.php";
    include "../../class/member.class.php";
    include "../../class/date.class.php";
    $mysqli=  connect();
    $cropID=$_POST["cropID"];
    $costID=$_POST["costID"];
    
    $dateClass=new date();
    if(!is_array($cropID)){
        ?>
<div style="text-align: center;">กรุณาเลือก Crop</div>
            <?php
        exit();
    }else{
        foreach ($cropID as $key => $value) {
            $cropClass=new crop($value);
            $crop=$cropClass->getCrop();
            $startDate[]=$crop["startDate"];
            $farmClass=new farm($crop["farmID"]);
            $farm=$farmClass->getFarm();
            $memberClass=new member($farm["memID"]);
            $member=$memberClass->getMember();
            $memName[]=$memberClass->memName();
            
            $riceObj=new rice($crop["rvID"]);
            $rice=$riceObj->getRice();
            $rvName[]=$rice["rvName"];
            
            if($costID==""){
                $val[]=$cropClass->sumCost();
            }else{
                $val[]=$cropClass->sumCost($costID);
            }
        }
    }
?>
<center>
    แผนภูมิแท่งเปรียบเทียบต้นทุนรวมในการปลูกข้าว<br>
    <span style="font-size: 80%;">
        <?php
            if($costID==''){
                echo "ต้นทุนรวม";
            }else{
                $query="
                    select  *
                    from    cost_list
                    where   `costID`='".$costID."'
                    ";
                $result=$mysqli->query($query);
                $row=$result->fetch_array(MYSQLI_ASSOC);
                echo "เฉพาะต้นทุน ".$row["costName"];
            }
        ?>
    </span>
</center>
<br>
<table class="noSpacing"style="width: 100%;height: 100%;">
    <tr>
        <td style="vertical-align: middle">
            <div style="border: none;margin-left: auto;margin-right: auto;display: table;">
                <table class="noSpacing" style="margin-left: 30px;margin-right: 30px;margin-top: 20px;">
                    <tr>
                        <td style="vertical-align: top;border-right: 1px solid #000;">
                            <div style="position: relative;height: 100%;">
                                <div style="position: absolute;top: -25px;left:-18px;font-size: 60%;white-space: pre;">บาท/ไร่</div>
                            </div>
                        </td>
                        <?php
                            $maxVal=max($val);
                            $maxHeight=250;
                            for($i=0;$i<count($val);$i++){
                                if($maxVal==0){
                                    $height=0;
                                }else{
                                    $height=((100*$val[$i])/$maxVal)*($maxHeight/100);
                                }
                                ?>
                        <td style="vertical-align: bottom;text-align: center;font-size: 80%;padding: 5px;">
                            <?=  number_format($val[$i])?>
                            <div 
                                style="
                                    background-color: #428bca;
                                    width: 30px;
                                    height: <?=$height?>px;
                                    margin-left: auto;
                                    margin-right: auto;
                                "></div>
                        </td>
                                    <?php
                            }
                        ?>
                    </tr>
                    <tr>
                        <td></td>
                        <?php
                            for($i=0;$i<count($val);$i++){
                                ?>
                        <td style="vertical-align: bottom;text-align: center;font-size: 80%;padding: 5px;border-top: 1px solid #000;">
                            <?=$i+1?>
                        </td>
                                    <?php
                            }
                        ?>
                    </tr>
                    <tr>
                        <td colspan="<?=count($val)+1?>" style="text-align: center;font-size: 80%;">รายที่</td>
                    </tr>
                </table>
            </div>
            <div style="border: 1px solid #428bca;margin-top: 10px;margin-left: auto;margin-right: auto;display: table;padding: 0px;font-size: 80%;">
                <table class="noSpacing" style="">
                    <tr style="text-align: center;background-color: #428bca;color: #fff;">
                        <td style="padding: 5px 10px 5px 10px;">รายที่</td>
                        <td style="padding: 5px 10px 5px 10px;">ชื่อ นามสกุล / วันที่เริ่มปลูก</td>
                        <td style="width: 150px;padding: 5px 10px 5px 10px;">ต้นทุน (บาท/ไร่)</td>
                    </tr>
                    <?php
                        for($i=0;$i<count($val);$i++){
                            ?>
                    <tr>
                        <td style="padding: 5px;text-align: center;border-bottom: 1px dashed #aaa;"><?=$i+1?></td>
                        <td style="padding: 5px 10px 5px 10px;border-bottom: 1px dashed #aaa;">
                            <?=$memName[$i]?> (<?=$rvName[$i]?>)
                            <div style="font-size: 80%;"><?=$dateClass->dateEncodeBE($startDate[$i])?></div>
                        </td>
                        <td style="padding: 5px;text-align: right;border-bottom: 1px dashed #aaa;"><?=  number_format($val[$i]);?></td>
                    </tr>
                                <?php
                        }
                    ?>
                </table>
            </div>
        </td>
    </tr>
</table>